from eventServer import eventServer
from collectCube import CollectCube
import math
from soundServer import sound

class Windmill(CollectCube):
    
    def __init__(self):
        CollectCube.__init__(self)
        
        self.setVar("texture", "windmill")
        
        self.setVar("timedItem", True)
        self.setVar("lifeSpan", 15)
        self.setVar("lifeBlinkTime", 2)
        
        self.wind_angle = 0
        self.wind_av    = 5
        self.wind_rad   = 4
        self.wind_y     = 0
           
    def onPickUp(self, player):
        playP = player.getVar("pos")
        self.wind_y = playP.y
         
    def update(self, cmd, args):
        CollectCube.update(self, cmd, args)
        
        player = self.getVar("itemPlayer")
        if(not(player == None)):
            p     = self.getVar("pos")
            playP = player.getVar("pos")
            
            self.wind_angle += self.wind_av * args['dtime']
            if(self.wind_angle > 2 * math.pi):
                self.wind_angle -= 2 * math.pi
            p.x = playP.x + (self.wind_rad * math.cos(self.wind_angle))
            
            self.wind_y -= (self.wind_y - playP.y)/24.0
            p.y = self.wind_y
                                
    def onCollision(self, dist, vec, side, sideType, above, toward, ignoreSurface, object):
        CollectCube.onCollision(self, dist, vec, side, sideType, above, toward, ignoreSurface, object)

        p = self.getVar("itemPlayer")
        if(not(p == None)):
            if(not(p == object)):
                if(object.hasVar("base")):
                    b = object.getVar("base")
                    if(b == 'player'):
                        fromPlayer = p.getVar("player")
                        toPlayer = object.getVar("player")
                        eventServer.pull('playerStomp',{'fromPlayer':fromPlayer,'toPlayer':toPlayer})
                        sound.play("windmill_kill")
